package com.luchao.ptz.utils;


import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class UbbtohtmlUtil {
    static Pattern p1 = Pattern.compile("<([^>]*)>", Pattern.DOTALL);

    public static String clearHtmlTag(String s) {
        try {
            Matcher m = null;
            m = p1.matcher(s);
            while (m.find()) {
                for (int i = 1; i <= m.groupCount(); i++) {
                    //System.out.println("找到 = " + m.group());
                    s = s.replaceAll(m.group(), "");
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return s.trim();
    }

    public static String delHtmlTag(String str) {
        int index1 = -1;
        int index2 = -1;
        while (true) {
            index1 = str.indexOf("<");
            index2 = str.indexOf(">", index1);
            if (index1 == -1) {
                break;
            }
            if (index2 == -1) {
                index2 = str.length();
            } else {
                index2 = index2 + 1;
            }
            str = str.substring(0, index1)
                    + str.substring(index2, str.length());

        }
        str = str.replaceAll("\r", "<br/>");
        str = str.replaceAll("\t", "    ");
        return str;
    }

    /**
     * 转换UBB
     *
     * @param text
     * @return
     */
    public static String UbbDecode(String text) {
        text = text.replaceAll("\\n", "<br>");

        text = replace(text, "backcolor(.*?)", "backcolor", "<font style=\"background-color:$2\">", "</font>");

        text = replace(text, "tr(.*?)", "tr", "<tr>", "</tr>");

        text = replace(text, "td(.*?)", "td", "<td>", "</td>");

        text = replace(text, "table(.*?)", "table", "<table>", "</table>");

        text = replace(text, "align=(.*?)", "align", "<p align=$2>", "</align>");

        text = replace(text, "url=(.*?)", "url", "<a href=\"$2\" target=_black>",
                "</a>");

        text = replace(text, "img", "img", "<img border=0 src=", "></img>");

        text = replace(text, "img=(.+?),(.+?)", "img",
                "<img width=\"$2\" hight=\"$3\" border=0 src=",
                "></img>");

        text = replace(text, "size=(.+?)", "size", "<font size=$2>", "</font>");

        text = replace(text, "font=(.+?)", "font", "<font face=$2>", "</font>");

        text = replace(text, "color=(.+?)", "color", "<font color=$2>",
                "</font>");

        text = replace(text, "email=(.+?)", "email", "<a href='mailto:$2' target=_black>", "</a>");

        text = replace(text, "u", "<u>", "</u>");
        text = replace(text, "i", "<i>", "</i>");
        text = replace(text, "li", "<li>", "</li>");
        text = replace(text, "list=(.*?)", "<ol>", "</ol>");
        text = replace(text, "list(.*?)", "<ul>", "</ul>");
        text = replace(text, "\\*", "<li>", "</li>");
        text = replace(text, "ol", "<ol>", "</ol>");
        text = replace(text, "ul", "<ul>", "</ul>");
        text = replace(text, "b", "<b>", "</b>");
        text = replace(text, "h1", "<h1>", "</h1>");
        text = replace(text, "h2", "<h2>", "</h2>");
        text = replace(text, "h3", "<h3>", "</h3>");
        text = replace(text, "h4", "<h4>", "</h4>");
        text = replace(text, "h5", "<h5>", "</h5>");
        text = replace(text, "h6", "<h6>", "</h6>");

        //处理不了的
        text = text.replaceAll("\\[url\\]", "<a>");

        return text;
    }

    public static String replace(String text, String reg, String replaceStr1,
                                 String replaceStr2) {
        return replace(text, reg, reg, replaceStr1, replaceStr2);
    }

    public static String replace(String text, String reg, String regEnd,
                                 String replaceStr1, String replaceStr2) {
        Matcher m = null;

        m = Pattern.compile(
                "(\\[" + reg + "\\])",
                Pattern.DOTALL | Pattern.CASE_INSENSITIVE | Pattern.MULTILINE)
                .matcher(text);
        text = m.replaceAll(replaceStr1);

        m = Pattern.compile(
                "(\\[/" + regEnd + "\\])",
                Pattern.DOTALL | Pattern.CASE_INSENSITIVE | Pattern.MULTILINE)
                .matcher(text);
        text = m.replaceAll(replaceStr2);

        return text;
    }

    public static void main(String arg[]) {
        String str = "[attachment=1619]用途：\n" +
                "1.主播登录粉丝游戏账号\n" +
                "2.代打登号\n" +
                "3.设备没装微信\n" +
                "4.不想登录自己微信\n" +
                "这个时候，就可以通过扫码登录游戏账号。\n" +
                " \n" +
                "游戏支持：\n" +
                "英雄联盟手游扫码登录\n" +
                "王者荣耀扫码登录\n" +
                "和平精英扫码登录\n" +
                "金铲铲之战扫码登录\n" +
                "天天爱消消扫码登录\n" +
                "光与夜之恋扫码登录\n" +
                "天天炫斗扫码登录\n" +
                "鸿图之下扫码登录\n" +
                "PUBG扫码登录\n" +
                "穿越火线扫码登录\n" +
                "QQ飞车扫码登录\n" +
                "第五人格扫码扫码登录\n" +
                "部落冲突扫码登录\n" +
                "FIFA足球扫码登录\n" +
                "QQ炫舞扫码登录\n" +
                "等...\n" +
                " \n" +
                "[b]地址[/b]：[url=http://jabi.cc/]》》》传送门《《《[/url] \n" +
                " \n" +
                "[size=4][b]使用教程[/b][/size]\n" +
                "打开本页面后，让好友使用微信直接扫码登录，即可自动跳转到游戏中并登录上好友的微信账号。\n" +
                "&nbsp;&nbsp;\n" +
                "[b][size=3]注意事项[/size][/b]\n" +
                "[color=#616264]1、请使用 [/color][color=#ff0000]Safari 浏览器[/color][color=#616264]打开本页面进行扫码登录，其它浏览器可能会出现无法跳转的情况；[/color]\n" +
                "2、必须要打开微信使用摄像头识别二维码，请勿在微信中长按识别；\n" +
                "3、如果二维码不显示、识别失败或过期，刷新本页重试即可。\n" +
                " \n" +
                " \n" +
                " \n";
        System.out.println(UbbDecode(str));
    }
}
